
import unittest
from alg import Solution
from gen import generate_max_product_test_cases


def find_max_product_subarray(nums):
    n = len(nums)
    max_prod = float('-inf')
    start_index = 0
    end_index = 0
    for i in range(n):
        current_prod = 1
        for j in range(i, n):
            current_prod *= nums[j]
            if current_prod > max_prod:
                max_prod = current_prod
                start_index = i
                end_index = j
    return max_prod, nums[start_index:end_index + 1]


class TestMaxProduct(unittest.TestCase):

    def test_example_cases(self):
        # 测试示例用例
        test_cases = [
            ([2, 3, -2, 4], 6),
            ([-2, 0, -1], 0),
            ([-2, 3, -4], 24),
            ([0, 2], 2),
            ([-1, -2, -3], 6)
        ]

        solution = Solution()

        for nums, expected in test_cases:
            with self.subTest(nums=nums):
                result = solution.maxProduct(nums)
                max_prod, subarray = find_max_product_subarray(nums)
                print(f"输入: nums = {nums}")
                print(f"输出: {result}")
                print(f"解释: 子数组 {subarray} 有最大乘积 {max_prod}。")
                self.assertEqual(result, expected)

    def test_random_cases(self):
        # 测试随机生成的用例
        test_cases = generate_max_product_test_cases(10)

        solution = Solution()

        for nums in test_cases:
            with self.subTest(nums=nums):
                result = solution.maxProduct(nums)
                max_prod, subarray = find_max_product_subarray(nums)
                print(f"输入: nums = {nums}")
                print(f"输出: {result}")
                print(f"解释: 子数组 {subarray} 有最大乘积 {max_prod}。")
                # 计算预期结果
                if not nums:
                    expected = 0
                else:
                    n = len(nums)
                    max_prod = float('-inf')
                    for i in range(n):
                        current_prod = 1
                        for j in range(i, n):
                            current_prod *= nums[j]
                            if current_prod > max_prod:
                                max_prod = current_prod
                    expected = max_prod

                self.assertEqual(result, expected)


if __name__ == '__main__':
    unittest.main()